package com.etime.shycourse.controller.support;

import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.orde.dto.OrderDetails;
import com.etime.shycourse.orde.pojo.Buycar;
import com.etime.shycourse.orde.pojo.Orders;
import com.etime.shycourse.orde.services.interfaces.BuycarServices;
import com.etime.shycourse.orde.services.interfaces.WxOrdersServices;
import com.etime.shycourse.wx.applets.dto.LessonDto;
import com.etime.shycourse.wx.applets.pojo.Course;
import com.etime.shycourse.wx.applets.pojo.Lesson;
import com.etime.shycourse.wx.applets.services.interfaces.LessonServices;
import com.etime.shycourse.wx.applets.services.interfaces.WxCourseServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName : WxSupportCourseController  //类名
 * @Description :   Wx支招 课程控制器//描述
 * @Author : W //作者
 * @Date: 2022/2/28  15:13
 */
@Api(tags = "微信-支招-课程数据接口")
@RestController
@RequestMapping("/wx/support/applets")
public class WxSupportCourseController {
    @Autowired
    private WxCourseServices service;
    @Autowired
    private WxOrdersServices ordersServices;
    @Autowired
    private BuycarServices buycarServices;
    @Autowired
    private LessonServices lessonServices;

    @ApiOperation("进入支招点开始学习，查询课程的基本信息 简介")
    @GetMapping("/get/{courseid}")
    public PojoMessage<Course> selectCourseBy(@PathVariable("courseid") Integer courseid,HttpServletRequest request) {
        PojoMessage<Course> msg = new PojoMessage<> ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        Course data = service.selectCourseBy (courseid);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到到课程详细信息");
        }
        return msg;
    }

    @ApiOperation("支招课程是否购买 携带token")
    @GetMapping("/ifCourse/{CourseId}")
    public BaseMessage IfCourseByCat(@PathVariable Integer CourseId, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        Integer i = service.IfCourseByCat (CourseId, token);
        if (i != null && i > 0) {
            msg.setCode (1);
            msg.setMsg ("该用户已经购买了该课程");
        } else {
            msg.setCode (0);
            msg.setMsg ("该用户没有购买课程");
        }
        return msg;
    }

    @ApiOperation("根据课程id 进行查询课时 进入支招专栏课程详情中，课时数据的初始化 whetherToBuy是否购买的判定值  1=已经购买 0=未购买 ")
    @GetMapping("/Lesson/get/{CourseId}")
    public ListMessage<LessonDto> selectLessonByCourseId(@PathVariable Integer CourseId, HttpServletRequest request) {
        ListMessage<LessonDto> msg = new ListMessage<> ();
        String token = request.getHeader ("token");
        List<LessonDto> data = service.selectLessonByCourseId (CourseId, token);
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课时信息");
        }
        return msg;
    }

    @ApiOperation("根据课程id 进行查询课时。 后台已经下单 ，课程已经购买了")
    @GetMapping("/Lessons/get/{CourseId}")
    public ListMessage<LessonDto> selectLessonByCourseIds(@PathVariable Integer CourseId, HttpServletRequest request) {
        ListMessage<LessonDto> msg = new ListMessage<> ();
        String token = request.getHeader ("token");
        List<LessonDto> data = service.selectLessonByCourseIds (CourseId, token);
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课时信息");
        }
        return msg;
    }

    @ApiOperation("创建订单 返回订单id")
    @PostMapping("/insert")
    @ApiImplicitParams({
            @ApiImplicitParam("orderdetail 、orders中的字段必须传一个，其他有根据前端需要传值,默认值的字段 可不传值"),
            @ApiImplicitParam(name = "orderuserid", value = "token携带"),
            @ApiImplicitParam(name = "ordercreateuserid", value = "不传 设置订单创建人 登录用户id"),
            @ApiImplicitParam(name = "orderusertruename", value = "不传 设置购买人姓 登录用户姓名"),
            @ApiImplicitParam(name = "ordercreatename", value = "不传 设置订单创建人姓名 登录用户姓名"),
            @ApiImplicitParam(name = "orderupdatetime", value = "不传 订单最后修改时间"),
            @ApiImplicitParam(name = "orderno", value = "不传 订单号"),
            @ApiImplicitParam(name = "orderstate", value = "可不传  默认1 -待支付"),
            @ApiImplicitParam(name = "ordercreatereason", value = "可传 有默认值：用户下单"),
            @ApiImplicitParam(name = "orderid", value = "不传 后台交互自动生成"),
    })
    public BaseMessage insertOrderDetail(@RequestBody OrderDetails orderDetails, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        int OrderId = ordersServices.insertSupportOrderDetails (orderDetails, token);
        if (OrderId != 0) {
            msg.setCode (OrderId);
            msg.setMsg ("创建订单成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("购买课程失败,可能原因：网络延迟、您已购买或者已创建订单！");
        }
        return msg;
    }

    /**
     * 根据订单id 获取订单详情
     * selectOrderDetailsByOrderId
     *
     * @param orderId
     * @return Orders
     */
    @ApiOperation("根据订单id 获取订单详情")
    @GetMapping("/OrderDetails/get/{orderId}")
    public PojoMessage<Orders> selectOrderDetailsById(@PathVariable Integer orderId) {
        PojoMessage<Orders> msg = new PojoMessage<> ();
        Orders data = ordersServices.selectSupportOrderDetailsByOrderIds (orderId);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到订单详情信息");
        }
        return msg;
    }

    /**
     * 加入购物车数据
     * insetOrUpdateBuyCar
     *
     * @param buyCarList
     * @return int 影响数据的条数 0 失败 1成功
     * int insetOrUpdateBuyCar(Buycar buycar);
     */
    @ApiOperation("创建购物车")
    @PostMapping("/buyCar/insert")
    public BaseMessage insetSupportOrUpdateBuyCar(@RequestBody List<Buycar> buyCarList, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        int rtn = buycarServices.insetSupportOrUpdateBuyCar (buyCarList, token);
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("创建购物车成功");
        } else {
            msg.setMsg ("创建购物车失败");
        }
        return msg;
    }

    /**
     * 判断用户有没有
     * 未购买的订单有返回订单id
     * 当用户 有订单存在 返回第一个订单id
     * 没有返回0
     *
     * @return int
     */
    @ApiOperation("(携带token)当用户,有订单存在,返回第一个订单id ")
    @GetMapping("/buyCar/Orders/user")
    public BaseMessage selectByCatOrdersByUserIds(HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        int rtn = ordersServices.selectByCatOrdersByUserId (token);
        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("有订单存在,已返回了第一个订单id");
        } else {
            msg.setCode (0);
            msg.setMsg ("没有订单存在");
        }
        return msg;
    }

    /**
     * 根据 课时id查询到视频文本
     * selectByLessonId
     *
     * @return Lesson
     */
    @ApiOperation("根据 课时id查询到视频+文本")
    @GetMapping("/lesson/get/Id/{LessonId}")
    public PojoMessage<Lesson> selectByLessonId(@PathVariable Integer LessonId) {
        PojoMessage<Lesson> msg = new PojoMessage<> ();
        Lesson data = lessonServices.selectByLessonId (LessonId);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到视频文本信息");
        }
        return msg;
    }
}

